var express = require("express"),
	router = express.Router(),
	isLogin = require("./public/is-login"),
	column = require("../../models/adminSite/column"),
	column = new column(),
	article = require("../../models/adminSite/article"),
	articleFun = new article();
//文章列表
router.get("/article" , isLogin.checkLogin , function(req , res){
	articleFun.getArticle({} , function(error , data){
		if(data){
			res.render("adminSite/articleList" , {
				title : "文章列表",
				article : data
			});
		};
	});
});
//文章列表
router.get("/addArticle" , isLogin.checkLogin , function(req , res){
	column.getColumn({} , function(error , data){
		if(error){
			return console.log(error);
		};
		return res.render("adminSite/addArticle" , {
			title : "添加文章",
			column : data
		});
	});
});
//添加文章
router.post("/addArticle" , isLogin.checkLogin , function(req , res){
	var data = req.body,
			res = res,
			articleFun = new article(data);
	if(data){
		articleFun.createArticle(function(error , data){
			if(error){
				return res.json({
					state : "error",
					message : "文章添加失败"
				});
			};
			//成功
			return res.json({
				state : "ok",
				message : "发布成功"
			});
		});
	};
});
//删除文章
router.post("/remove" , isLogin.checkLogin , function(req , res){
	var data = req.body,
			articleFun = new article();
	articleFun.remove({_id : data.id} , function(error , data){
			if(error){
				return res.json({
					state : "error",
					message : "删除失败"
				});
			};
			if(data){
				return res.json({
					state : "ok",
					message : "删除成功"
				});
			};
	});
});
//接口
module.exports = router;
